package com.ruoyi.dynamic.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.CommonUtils;
import com.ruoyi.dynamic.mapper.RoomMaiRecordMapper;
import com.ruoyi.dynamic.model.constant.DynamicConstant;
import com.ruoyi.dynamic.model.pojo.entity.RoomMaiRecord;
import com.ruoyi.dynamic.service.IRoomMaiRecordService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 房间上麦记录表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2024-11-26
 */
@Service
public class RoomMaiRecordServiceImpl extends ServiceImpl<RoomMaiRecordMapper, RoomMaiRecord> implements IRoomMaiRecordService {

    @Async
    @Override
    public void add(Long roomId, String roomNumber, Long userId, Integer maiNum, Integer type, Integer status, String photo, String nickName, Integer star) {
        RoomMaiRecord roomMaiRecord = lambdaQuery()
                .eq(RoomMaiRecord::getRoomId, roomId)
                .eq(RoomMaiRecord::getUserId, userId)
                .eq(RoomMaiRecord::getStatus, status)
                .one();
        if (CommonUtils.isNull(roomMaiRecord)) {
            roomMaiRecord = new RoomMaiRecord();
        }
        roomMaiRecord.setMoney(BigDecimal.ZERO);
        roomMaiRecord.setRoomId(roomId);
        roomMaiRecord.setUserId(userId);
        roomMaiRecord.setRoomNumber(roomNumber);
        roomMaiRecord.setStatus(status);
        roomMaiRecord.setType(type);
        roomMaiRecord.setMaiNum(maiNum);
        roomMaiRecord.setUserNickName(nickName);
        roomMaiRecord.setUserPhoto(photo);
        roomMaiRecord.setStarLocation(star);
        saveOrUpdate(roomMaiRecord);
    }


    @Override
    public Integer getRoomMaiNum(Long roomId, Integer roomTypeId) {
        RoomMaiRecord one = lambdaQuery()
                .ne(roomTypeId.equals(DynamicConstant.RoomType.Heart_Carve), RoomMaiRecord::getMaiNum, 7)
                .eq(RoomMaiRecord::getRoomId, roomId)
                .eq(RoomMaiRecord::getStatus, DynamicConstant.CheckStatus.NO)
                .orderByDesc(RoomMaiRecord::getMaiNum)
                .last("limit 1").one();
        if (one == null) return 1;
        return one.getMaiNum() + 1;
    }
}
